/**
 * Created with IntelliJ IDEA.
 * User: zzl
 * Date: 12-11-18
 * To change this template use File | Settings | File Templates.
 */
(function (global, undefined) {

    global.domex = {

        clearSelection: function () {
            if (window.getSelection) {
                if (window.getSelection().empty) {  // Chrome
                    window.getSelection().empty();
                } else if (window.getSelection().removeAllRanges) {  // Firefox
                    window.getSelection().removeAllRanges();
                }
            } else if (document.selection) {  // IE?
                try {
                    document.selection.empty();
                }
                catch (e) {
                    //quiet
                }
            }
        },

        setSelection: function (elem, start, end) {
            if (elem.createTextRange) {
                var range = elem.createTextRange();
                range.collapse(true);
                range.moveStart('character', start);
                range.moveEnd('character', (end || start) - start);
                range.select();
            }
            else {
                elem.selectionStart = start;
                elem.selectionEnd = end || start;
            }
        },

        getSelection: function (el) {
            var start, end;
            if (el.selectionStart) {
                start = el.selectionStart;
                end = el.selectionEnd;
            } else if (el.createTextRange) {
                var bm = document.selection.createRange().getBookmark();
                var tr = el.createTextRange();
                tr.moveToBookmark(bm);

                var tr2 = el.createTextRange();
                tr2.collapse(true);
                tr2.setEndPoint("EndToStart", tr);
                start = tr2.text.length;
                end = tr2.text.length + tr.text.length;
            }
            return {
                start: start,
                end: end
            }
        },

        getWidth: function (e, withPadding) {
            if (e.length) {
                e = e[0];
            }
            var result = parseInt(e.style.width) || parseInt($(e).css('width'));
            if (withPadding) {
                result += parseInt(e.style.paddingLeft) || parseInt($(e).css('padding-left')) || 0;
                result += parseInt(e.style.paddingRight) || parseInt($(e).css('padding-right')) || 0;
            }
            return result;
        },

        selectAllOnFocus: function (elem) {
            $(elem).select().on('mouseup.selectAllOnFocus', function (e) {
                e.preventDefault();
                $(this).off("mouseup.selectAllOnFocus");
            });
        },

        getViewport: function () {
            var $window = $(window);
            var rect = {
                x1: $window.scrollLeft(),
                y1: $window.scrollTop()
            };
            rect.x2 = rect.x1 + $window.width();
            rect.y2 = rect.y1 + $window.height();
            return rect;
        },

        getCurrentStyle: function (el, name) {
            var styles = el.currentStyle;
            if (!styles) {
                styles = document.defaultView.getComputedStyle(el, null);
            }
            return name ? styles[name] : styles;
        },

        inDomTree: function (el, andVisible) {
            if (!el) {
                return false;
            }
//            var testEl = el;
//            while (testEl = testEl.parentNode) {
//                if (testEl === document) {
//                    if(andVisible) {
//                        return domex.getCurrentStyle(el, 'display') !== 'none';
//                    }
//                    return true;
//                }
//            }
            if (!document.documentElement.contains(el)) {
                return false;
            }
            if (andVisible) { // not considering parent visible..
                return domex.getCurrentStyle(el, 'display') !== 'none';
            }
            return true;
        },

        isVisible: function(el) {
            return domex.getCurrentStyle(el, 'display') !== 'none';
        },

        getFirstChildElelement: function (parentEl, nodeName) {
            //*
            if(!('firstElementChild' in parentEl)) {
                var child = parentEl.firstChild;
                while (child) {
                    if (child.nodeType == 1 &&
                        (nodeName === undefined || child.nodeName === nodeName)) {
                        return child;
                    }
                    child = child.nextSibling;
                }
                return null;
            }
            //*/
            /*todo: check nodeName*/
            var el = parentEl.firstElementChild;
            while(el) {
                if(nodeName === undefined || el.nodeName === nodeName) {
                    return el;
                }
                el = domex.getNextElement(el);
            }
            return null;
        },

        getChildElelements: function (parentEl, nodeName) {
            var child = parentEl.firstChild;
            var result = [];
            while (child) {
                if (child.nodeType == 1 &&
                    (nodeName === undefined || child.nodeName == nodeName)) {
                    result.push(child);
                }
                child = child.nextSibling;
            }
            return result;
        },

        getNextElement: function (el) {
            //*
            if(!('nextElementSibling' in el)) {
                var next = el.nextSibling;
                while (next && next.nodeType != 1) {
                    next = next.nextSibling;
                }
                return next;
            }
            //*/
            return el.nextElementSibling;
        },

        getPrevElement: function (el) {
            //*
            if(!('previousElementSibling' in el)) {
                var prevEl = el.previousSibling;
                while (prevEl && prevEl.nodeType != 1) {
                    prevEl = prevEl.previousSibling;
                }
                return prevEl;
            }
            //*/
            return el.previousElementSibling;
        }


    };
})(window);